package org.dioib.practice.controller;


import jakarta.annotation.Resource;
import org.dioib.practice.mapper.DiscardSysUserMapper;
import org.dioib.practice.model.SysUser;
import org.dioib.practice.service.impl.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/rest/users")
public class SysUserControllerRest {

    @Autowired
    SysUserService sysUserServiceImp;

    @Autowired
    private DiscardSysUserMapper discardSysUserMapper;


    @GetMapping
    public List<SysUser> getAllSysUser() {
        return discardSysUserMapper.findAll();
    }
    @GetMapping("/{name}")
    public List<SysUser> getSysUser(@PathVariable String name) {
        return discardSysUserMapper.findOne(name);
    }

    @PostMapping
    public Integer addSysUser(SysUser sysUser) {
//        return sysUserMapper.insert(sysUser);
        System.out.println("【“+this.getClass().getEnclosingMethod().getName()+”】");
        return sysUserServiceImp.save(sysUser);
    }

    @PutMapping
    public Integer putSysUser(SysUser sysUser) {
//        return sysUserMapper.updateSysUser(sysUser);
        System.out.println("【“+this.getClass().getEnclosingMethod().getName()+”】");
        return sysUserServiceImp.save(sysUser);
    }

    @DeleteMapping("/{id}")
    public Integer delSysUser(@PathVariable Long id) {
        SysUser user = new SysUser();
        user.setId(id);
        return discardSysUserMapper.delete(user);
    }

    /**
     *  （当前页码-1）*pageSize = 起始id
     * @param pageNum  页码
     * @param pageSize 每页大小
     * @return 用户列表
     */
//    @GetMapping
//    public Map<String, Object> findSysUserByPage(@PathParam("pageNum") Integer pageNum, @PathParam("pageSize") Integer pageSize, @RequestBody SysUser sysUser){
//        System.out.printf("【“+this.getClass().getEnclosingMethod().getName()+”】");
//        return sysUserServiceImp.findAll(pageNum, pageSize, sysUser);
//    }
//    @GetMapping
//    public Map<String, Object> findSysUserByPage(@PathParam("pageNum") Integer pageNum, @PathParam("pageSize") Integer pageSize, @PathParam("id") Integer id){
//        SysUser sysUser = new SysUser();
//        sysUser.setId(id);
//        System.out.printf("【“+this.getClass().getEnclosingMethod().getName()+”】");
//        return sysUserServiceImp.findAll(pageNum, pageSize, sysUser);
//    }

    //    }
    @GetMapping("/page")
    public Map<String, Object> findSysUserByPage(@RequestParam("pageNum") Integer pageNum,
                                                 @RequestParam("pageSize") Integer pageSize,
                                                 @RequestParam(required = false) String firstName){
        SysUser sysUser = new SysUser();
        sysUser.setFirstName(firstName);
        System.out.println("【“+this.getClass().getEnclosingMethod().getName()+”】");
        return sysUserServiceImp.findAll(pageNum, pageSize, sysUser);
    }

    //TODO: 1. 前端post请求
    //TODO: 2. MyBatis-Plus分页插件
    

}